package com.smartstate.iot.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.common.base.constants.GlobalsConstants;
import com.smartstate.common.base.exception.CommonException;
import com.smartstate.common.web.httpResult.ApiResultIgnore;
import com.smartstate.iot.dto.LogDateDto;
import com.smartstate.iot.dto.PageDTO;
import com.smartstate.iot.entity.TopicLog;
import com.smartstate.iot.service.TopicLogService;
import com.smartstate.iot.service.TopicService;
import com.smartstate.iot.util.ExcelUtils;
import com.smartstate.iot.vo.ResultVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/log")
public class TopicLogController {

    @Autowired
    private TopicLogService topicLogService;

    @Autowired
    private TopicService topicService;

    @PostMapping("/findPageTopicLog")
    @ApiOperation(value = "单表分页查询设备日志")
    @ApiResultIgnore
    public ResultVO<Page<TopicLog>> pageTopicLog(@RequestBody PageDTO<LogDateDto> pageDTO) {
        Page<TopicLog> topicLogPage = topicLogService.queryAllPageByDTO(pageDTO);
        return ResultVO.ok(topicLogPage);
    }

    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出用户信息excel")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        setExcelRespProp(response, "用户信息模板");
        List<TopicLog> list = topicLogService.list();
        EasyExcel.write(response.getOutputStream())
                .head(TopicLog.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("日子信息")
                .doWrite(list);
    }

    /**
     * 设备正常数量 ,异常数量, 离线数量 , 总数
     * @return
     */
    @GetMapping("/findDeviceType")
    @ApiResultIgnore
    public List<Integer> findDeviceType() {
        //查询网管设备总数
        List<Integer> allType = topicService.getAllType();
        return allType;
    }

    /**
     * 设置excel下载响应头属性
     */
    private void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    }

}
